﻿using Batman.Maintenance.DTO;
using Batman.Maintenance.DTO.Column;
using Batman.Maintenance.Utils;
using System;
using System.Linq.Expressions;

namespace Batman.Maintenance
{
    public static class MaintenanceMainTableExtension
    {
        /// <summary>
        /// 是否有多选列
        /// </summary>
        /// <param name="has"></param>
        public static MaintenanceMainTable<T> HasSelection<T>(this MaintenanceMainTable<T> MainTable, bool has = true) where T : class
        {
            MainTable.HasSelection = has;

            return MainTable;
        }


        /// <summary>
        /// 设置读取API
        /// </summary>
        /// <param name="url"></param>
        public static MaintenanceMainTable<T> TableReadAction<T>(this MaintenanceMainTable<T> MainTable, string url) where T : class
        {
            MainTable.ReadAction = url;

            return MainTable;
        }

        /// <summary>
        /// 添加列表字段
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="label"></param>
        /// <param name="expression"></param>
        public static MaintenanceMainTable<T> HasTextColumn<T, TResult>(this MaintenanceMainTable<T> MainTable, string label, Expression<Func<T, TResult>> expression) where T : class
        {
            var propName = ExpressionUtil.GetSmallHumpWord(expression);
            MainTable.Columns.Add(new TextColumn(label, propName));

            return MainTable;
        }

        public static MaintenanceMainTable<T> HasTextColumn<T>(this MaintenanceMainTable<T> MainTable, string label, string prop) where T : class
        {
            MainTable.Columns.Add(new TextColumn(label, prop));

            return MainTable;
        }

        /// <summary>
        /// 添加列表字段
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="label"></param>
        /// <param name="propExpression"></param>
        public static MaintenanceMainTable<T> HasImageColumn<T, TResult>(this MaintenanceMainTable<T> MainTable, string label, Expression<Func<T, TResult>> propExpression, string path = "") where T : class
        {
            var propName = ExpressionUtil.GetSmallHumpWord(propExpression);
            MainTable.Columns.Add(new ImageColumn(label, propName, path));

            return MainTable;
        }

        /// <summary>
        /// 添加列表字段
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="label"></param>
        /// <param name="expression"></param>
        public static MaintenanceMainTable<T> HasLinkColumn<T, TResult>(this MaintenanceMainTable<T> MainTable, string label, Expression<Func<T, TResult>> expression) where T : class
        {
            var propName = ExpressionUtil.GetSmallHumpWord(expression);
            MainTable.Columns.Add(new LinkColumn(label, propName));


            return MainTable;
        }


        /// <summary>
        /// 添加列表字段
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="label"></param>
        /// <param name="expression"></param>
        public static MaintenanceMainTable<T> HasSwitchColumn<T, TResult>(this MaintenanceMainTable<T> MainTable, string label, Expression<Func<T, TResult>> expression, SwitchColumn other = null) where T : class
        {
            var propName = ExpressionUtil.GetSmallHumpWord(expression);

            if (other != null)
            {
                other.Label = label;
                other.Prop = propName;
                MainTable.Columns.Add(other);
            }
            else
            {
                var column = new SwitchColumn(label, propName);
                MainTable.Columns.Add(column);
            }

            return MainTable;
        }
    }
}
